import 'package:banyou_app/app/config/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///登录输入框 中自定义widget
class LoginInput extends StatefulWidget {
  final String title;
  final String hint;
  final String defaulTiele;
  final bool isForgetPassword;
  final ValueChanged<String>? onChanged;
  final ValueChanged<bool>? focusChanged;
  final bool lineStretch;
  final bool obscureText;
  final bool autofocus;
  final TextInputType? keyBoardType;
  final Widget? rightWidget;
  final TextEditingController? controller;

  ///限制输入条件
  final List<TextInputFormatter>? inputFormatters;

  const LoginInput(
      {required this.title,
      this.hint = "",
      this.defaulTiele = "",
      this.isForgetPassword = false,
      this.onChanged,
      this.inputFormatters,
      this.focusChanged,
      this.lineStretch = false,
      this.obscureText = false,
      this.autofocus = true,
      this.rightWidget,
      this.controller,
      this.keyBoardType});

  @override
  _LoginInputState createState() => _LoginInputState();
}

class _LoginInputState extends State<LoginInput> {
  final _focusNode = FocusNode();
  late TextEditingController _tfController;
  @override
  void initState() {
    // TODO: implement initState
    if (widget.controller != null) {
      _tfController = widget.controller!;
    } else {
      _tfController = TextEditingController();
    }
    if (widget.defaulTiele != null) {
      _tfController.text = widget.defaulTiele;
    }
    super.initState();
    //是否获取光标的监听
    _focusNode.addListener(() {
      if (widget.focusChanged != null) {
        widget.focusChanged!(_focusNode.hasFocus);
      }
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            widget.title.length > 0
                ? Container(
                    padding: EdgeInsets.only(left: 15),
                    // width: 65.w,
                    child: Text(
                      widget.title,
                      style: TextStyle(
                          fontSize: 16.sp,
                          color: ColorConfig.body1_txt_color,
                          fontFamily: 'mononoki'),
                    ),
                  )
                : Container(),
            _input(),
            widget.isForgetPassword
                ? Container(
                    padding: EdgeInsets.only(left: 20.w, right: 20.w),
                    child: TextButton(
                      onPressed: () {
                        // Get.toNamed("/forget-pwd");
                      },
                      child: Text(
                        "忘记密码?",
                        style: TextStyle(
                          fontSize: 16.sp,
                        ),
                      ),
                      style: ButtonStyle(
                        //定义文本的样式 这里设置的颜色是不起作用的
                        textStyle: MaterialStateProperty.all(
                            TextStyle(fontSize: 38.sp, color: Colors.white)),
                        //更优美的方式来设置
                        foregroundColor: MaterialStateProperty.resolveWith(
                          (states) {
                            if (states.contains(MaterialState.focused) &&
                                !states.contains(MaterialState.pressed)) {
                              //获取焦点时的颜色
                              return Colors.grey;
                            } else if (states.contains(MaterialState.pressed)) {
                              //按下时的颜色
                              return Colors.grey;
                            }
                            //默认状态使用灰色
                            return Get.theme.primaryColor;
                          },
                        ),
                        //背景颜色
                        backgroundColor:
                            MaterialStateProperty.resolveWith((states) {
                          //设置按下时的背景颜色
                          if (states.contains(MaterialState.pressed)) {
                            return Colors.transparent;
                          }
                          //默认不使用背景颜色
                          return Colors.transparent;
                        }),
                        //设置水波纹颜色
                        overlayColor:
                            MaterialStateProperty.all(Colors.transparent),
                        //设置阴影  不适用于这里的TextButton
                        elevation: MaterialStateProperty.all(0),
                        //设置按钮内边距
                        padding: MaterialStateProperty.all(EdgeInsets.all(4)),
                        //设置按钮的大小
                        minimumSize: MaterialStateProperty.all(Size(20, 20)),

                        //外边框装饰 会覆盖 side 配置的样式
                        shape: MaterialStateProperty.all(StadiumBorder()),
                      ),
                    ),
                  )
                : widget.rightWidget != null
                    ? widget.rightWidget!
                    : Container()
          ],
        ),
      ],
    );
  }

  _input() {
    return Expanded(
      child: TextField(
        focusNode: _focusNode,
        maxLines: 1,
        controller: _tfController,
        onChanged: widget.onChanged,
        keyboardAppearance: Brightness.light,
        obscureText: widget.obscureText,
        keyboardType: widget.keyBoardType,
        autofocus: !widget.autofocus,
        cursorColor: Get.theme.primaryColor,
        inputFormatters: widget.inputFormatters,
        style: TextStyle(
            fontSize: 13.sp,
            fontWeight: FontWeight.w400,
            color: ColorConfig.text_label),
        //输入框样式
        decoration: InputDecoration(
            contentPadding:
                EdgeInsets.only(left: 10.w, right: 10.w, top: 0, bottom: 0),
            border: InputBorder.none,
            hintText: widget.hint,
            hintStyle: TextStyle(fontSize: 16.sp, color: Color(0xFFC9C9C9))),
      ),
    );
  }
}
